package com.u2ds.console.web;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.cybermkd.mongo.kit.MongoKit;
import com.cybermkd.mongo.kit.MongoQuery;
import com.cybermkd.mongo.plugin.MongoPlugin;
import com.mongodb.MongoClient;
import com.u2ds.console.web.util.TypeUtil;
import com.u2ds.core.util.DateUtil;
import com.u2ds.dss.entity.court.CourtInfo;
import com.u2ds.dss.entity.court.CourtProduct;
import com.u2ds.dss.entity.court.CourtTimeRule;
import com.u2ds.dss.entity.sys.SysResources;
import com.u2ds.dss.tools.ProductGenerateTool;
import com.u2ds.dss.tools.SessionTool;
import org.apache.catalina.manager.util.SessionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.u2ds.console.web.util.UrldistributeUtil;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SiteIndexController {

    private static Logger logger = LoggerFactory.getLogger(SiteIndexController.class);

    @RequestMapping("/")
    public String loginHome(HttpServletRequest request){
//        Calendar c = Calendar.getInstance();
//        c.set(Calendar.HOUR_OF_DAY, 10);
//        c.set(Calendar.MINUTE, 0);
//        c.set(Calendar.SECOND, 0);
        MongoQuery orderQuery = new MongoQuery();
        orderQuery.use("CourtOrder");
//        orderQuery.set("courtId", "59a3d04fd9fd2b18fce984e0")
//                  .set("startTime", c.getTime())
//                  .set("endTime", DateUtil.addHours(c.getTime(), 1d))
//                  .save();
//
//        orderQuery.set("courtId", "59a3d04fd9fd2b18fce984e0")
//                .set("startTime", DateUtil.addHours(c.getTime(), 2d))
//                .set("endTime", DateUtil.addHours(c.getTime(), 3d))
//                .save();
//
//        orderQuery.set("courtId", "59a3d04fd9fd2b18fce984e0")
//                .set("startTime", DateUtil.addHours(c.getTime(), 8d))
//                .set("endTime", DateUtil.addHours(c.getTime(), 10d))
//                .save();

        MongoQuery timeRuleQuery = new MongoQuery();
        List<JSONObject> courtTimeRules = timeRuleQuery.use("CourtTimeRule").eq("cardId", 1).ascending("serial").find();
        JSONObject courtInfo = new MongoQuery().use("CourtInfo").findOne();
        List<CourtProduct> products = new ProductGenerateTool().generateProducts(courtInfo, courtTimeRules, orderQuery.ascending("startTime").findAll(), new Date());
        return "index";
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        return "index";
	}

    @RequestMapping("/test")
    public String test(HttpServletRequest request){
        return "sys/test";
    }

	@ResponseBody
    @RequestMapping("/page.json")
	public Object BaseInfo(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",true);
        result.put("msg", "");
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> info = new HashMap<String, Object>();
        info.put("name", "内部演示");
        info.put("region", "jiangbei, chongqing");
        data.put("info", info);
        result.put("data",data);
        List<String> settings = new ArrayList<String>();
        settings.add("班级通讯录");
        settings.add("班级群组");
        settings.add("班级群组1");
        info.put("setting", settings);
        return result;
    }
//	public String home(){
//		Condition query = Condition.build();
		//添加普通查询条件
//		query.add("orgCode", "500010000");
//		List<DimOrgan> organList = this.dimOrganMapper.findByParams(query);
		//添加分页
//		PageBounds pageBounds = new PageBounds(1, 5);;
//		PageResult<DimOrgan> organPage = this.dimOrganMapper.findByPage(query, pageBounds);
		//添加排序
//		PageBounds pageBounds = new PageBounds(2, 5);
//		query.addOrder("orgCode", Direction.DESC);
//		PageResult<DimOrgan> organPage = this.dimOrganMapper.findByPage(query, pageBounds);
		//不等于
//		query.addWhere(ConditionConstant.PREFIX_NOT_EQ + "orgCode", "500001004");
//		PageBounds pageBounds = new PageBounds(1, 5);
//		PageResult<DimOrgan> organPage = this.dimOrganMapper.findByPage(query, pageBounds);
		//IN,NOT_IN
//		String[] orgArray = {"500001002","500001013","500001014","500001015","500001016","500001017","500001018"};
//		query.addWhere(ConditionConstant.PREFIX_NOT_IN + "orgCode", orgArray);
//		PageBounds pageBounds = new PageBounds(1, 5);
//		PageResult<DimOrgan> organPage = this.dimOrganMapper.findByPage(query, pageBounds);
		//大于、小于
//		query.addWhere(ConditionConstant.PREFIX_GT + "orgCode", "500001003");
//		PageBounds pageBounds = new PageBounds(1, 5);
//		PageResult<DimOrgan> organPage = this.dimOrganMapper.findByPage(query, pageBounds);
//		query.add("clevle", 2);
//		DimOrgan organ = this.dimOrganMapper.findUniqueByParams(query);

		
//		return "hello";
//	}
	/* 
	 Condition query = Condition.build();
		query.add("orgCode", "500010000");
		query.addWhere(ConditionConstant.PREFIX_NOT_EQ + "orgCode", "500010000");
		query.addOrder("clevel", Direction.ASC);
		PageBounds pageBounds = new PageBounds(0, 5);
		List<DimOrgan> organList = this.dimOrganMapper.findByPage(query, pageBounds);
		DimOrgan organ = this.dimOrganMapper.findUniqueByParams(query);
		for(DimOrgan organ : organList){
			System.out.println(organ.getOrgCode());
		}
		DimOrgan organ = new DimOrgan();
		organ.setOrgCode("500001100");
		organ.setOrgName("测试insert机构");
		dimOrganMapper.insert(organ);
	 */
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request, ModelMap model) {
		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
			subject.logout();
		}
		return "login";
	}

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, ModelMap model) {
        return "redirect:" + "login";
    }

	@RequestMapping("/authed")
	public String authed() {
		return "redirect:" + UrldistributeUtil.distribute(SecurityUtils.getSubject());
	}

	@RequestMapping("/unauthorized")
	public String unauthorized() {
		return "login";
	}
}
